package com.hoow.shakoow.shake

import android.annotation.SuppressLint
import android.os.*
import android.view.View
import android.widget.Toast
import androidx.core.content.ContextCompat
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.hoow.shakoow.R
import com.hoow.shakoow.ai.data.ReqSetUserName
import com.hoow.shakoow.ai.home.NetworkViewModel
import com.hoow.shakoow.base.BaseActivity
import com.hoow.shakoow.databinding.ActivitySetNickNameBinding
import com.hoow.shakoow.enums.PageTagEnum
import com.hoow.shakoow.extend.*
import com.hoow.shakoow.utils.analytics.util.AnalyticsUtil


/** 设置昵称页 */
@SuppressLint("CustomSplashScreen")
class SetNickNameActivity : BaseActivity() {

    //布局binding
    private lateinit var mActivityBinding: ActivitySetNickNameBinding

    // 性别
    private var mGender: String = "1"

    //api接口viewModel
    private val mNetworkViewModel by lazy {
        ViewModelProvider(this)[NetworkViewModel::class.java]
    }

    override fun onBindingViewModel(): List<ViewModel> {
        val list = mutableListOf<ViewModel>()
        list.add(mNetworkViewModel)
        return list
    }

    override fun onBindingViewTag(): String {
        return PageTagEnum.set_nick_name.name.uppercase()
    }

    override fun onBindingContentView(): View {
        mActivityBinding = ActivitySetNickNameBinding.inflate(layoutInflater)
        return mActivityBinding.root
    }

    @SuppressLint("SetTextI18n", "SourceLockedOrientationActivity")
    override fun onContentViewBinded() {
        try {
            // 设置昵称与性别回调
            mNetworkViewModel.onSetUserNameLiveData.observe(this) {
                if (it.isSuccess) {
                    // 去摇一摇页
                    startToShakePage()
                }
            }

            // 点击男
            mActivityBinding.llMaleBg.setOnClickListener {
                mGender = "1"

                mActivityBinding.llMaleBg.setBackgroundResource(R.drawable.bg_gender_selected)
                mActivityBinding.llFemaleBg.setBackgroundResource(R.drawable.bg_gender_unselect)
                mActivityBinding.imageGenderMale.setImageResource(R.drawable.icon_male)
                mActivityBinding.imageGenderFemale.setImageResource(R.drawable.icon_female_no)

                mActivityBinding.textMale.setTextColor(
                    ContextCompat.getColor(
                        this,
                        R.color.main_black
                    )
                )
                mActivityBinding.textFemale.setTextColor(
                    ContextCompat.getColor(
                        this,
                        R.color.form_border
                    )
                )

                // 打点：摇一摇
                AnalyticsUtil.saveHitPointInfo(
                    type = "SET_NICKNAME_MALE_CLICK",
                    remark = ""
                )
            }

            // 点击妇
            mActivityBinding.llFemaleBg.setOnClickListener {
                mGender = "0"

                mActivityBinding.llFemaleBg.setBackgroundResource(R.drawable.bg_gender_selected)
                mActivityBinding.llMaleBg.setBackgroundResource(R.drawable.bg_gender_unselect)
                mActivityBinding.imageGenderMale.setImageResource(R.drawable.icon_male_no)
                mActivityBinding.imageGenderFemale.setImageResource(R.drawable.icon_female)

                mActivityBinding.textMale.setTextColor(
                    ContextCompat.getColor(
                        this,
                        R.color.form_border
                    )
                )
                mActivityBinding.textFemale.setTextColor(
                    ContextCompat.getColor(
                        this,
                        R.color.main_black
                    )
                )

                // 打点：摇一摇
                AnalyticsUtil.saveHitPointInfo(
                    type = "SET_NICKNAME_FEMALE_CLICK",
                    remark = ""
                )
            }

            // 保存
            mActivityBinding.textConfirm.setOnClickListener {
                // 保存
                val name = mActivityBinding.editNickName.text.toString().trim()
                if (name.isNotBlank()) {
                    val reqSetUserName = ReqSetUserName(name = name, sex = mGender)
                    mNetworkViewModel.setUserName(reqSetUserName)
                } else {
                    Toast.makeText(this, "Please enter your nick name", Toast.LENGTH_SHORT)
                        .show()
                }

                // 打点：摇一摇
                AnalyticsUtil.saveHitPointInfo(
                    type = "SET_NICKNAME_SAVE_CLICK",
                    remark = ""
                )
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}